//
//  LoginViewController.m
//  UI_豆瓣
//
//  Created by dllo on 15/10/27.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "LoginViewController.h"
#import "LoginView.h"
#import "RegisterViewController.h"
#import "User.h"
#import "DataBaseSingleng.h"
@interface LoginViewController ()

@property (nonatomic, retain)LoginView *loginView;


@end

@implementation LoginViewController

- (void)dealloc
{
    [_loginView release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.loginView = [[LoginView alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
    self.loginView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.loginView];
    [_loginView release];
    
    
    //设置标题
    self.navigationItem.title = @"用户登陆";
    
    //设置左侧按钮
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(leftButton:)];
    
    //注册按钮方法
    [self.loginView.registButton addTarget:self action:@selector(aaa:) forControlEvents:UIControlEventTouchUpInside];
    
    //登录按钮方法
     [self.loginView.logButton addTarget:self action:@selector(qqq:) forControlEvents:UIControlEventTouchUpInside];
    
   
}
//登录按钮
- (void)qqq:(UIButton *)but{

    
        NSMutableArray *array = [[DataBaseSingleng shareDataBase]selectUser];
   
    BOOL result = NO;
    for (User *user in array) {
        if ([self.loginView.nameField.text isEqualToString:user.userName]  && [self.loginView.passwordField.text isEqualToString:user.passworld]) {
            
            result = YES;
        }
    
    }
    
        if (result == YES) {
            
            [[NSUserDefaults standardUserDefaults] setObject:@"1" forKey:@"isLogin"];
                        
            UIAlertController *alterC = [UIAlertController alertControllerWithTitle:@"提示" message:@"登录成功" preferredStyle:UIAlertControllerStyleAlert];
            
            [[NSUserDefaults standardUserDefaults] setObject:self.loginView.nameField.text forKey:@"userName"];
            
            [self presentViewController:alterC animated:YES completion:^{
                
                
            }];
            
            UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                
                //收键盘
                [self.loginView.passwordField resignFirstResponder];

              
                [self dismissViewControllerAnimated:YES completion:^{
                    
                    
                }];
                
            }];
            
            UIAlertAction *channelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                
                
            }];
            
            [alterC addAction:sureAction];
            [alterC addAction:channelAction];
            
        }
        else
        {
            
            UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"提示" message:@"账号或密码错误" preferredStyle:UIAlertControllerStyleAlert];
            
            [self presentViewController:alertC animated:YES completion:^{
                
            }];
            // 创建提示框按钮
            UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                
            }];
            // 将按钮放在提示框上
            [alertC addAction:action];
        }
        
}




//注册按钮方法实现
- (void)aaa:(UIButton *)btn{
    RegisterViewController *regusterVC = [[RegisterViewController alloc]init];
    
    [self.navigationController pushViewController:regusterVC animated:YES];
    
}



//导航栏左侧按钮方法
-(void)leftButton:(UIBarButtonItem *)but
{
    [self dismissViewControllerAnimated:YES completion:^{
        
        
    }];
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
